$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/feedback/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true,hidden:true },
            { label: '用户名', name: 'username', index: 'username', width: 80 },
			{ label: '图片', name: 'imgurl', index: 'imgurl', width: 150,formatter: function(cellvalue, options, rowdata) {
                    if(rowdata.imgurl!=null&&rowdata.imgurl!=''){
                        var tp = "";
                        var array = rowdata.imgurl.split(",");


                        for(var i=0;i<array.length;i++){
                            tp +='<img src='+array[i]+' width="50" height="50"/>&nbsp;&nbsp;';
                        }
                        return tp;
                    }else{
                        return '';
                    }

                } },
			{ label: '手机号', name: 'telphone', index: 'telphone', width: 80 }, 			
			{ label: '添加时间', name: 'createtime', index: 'createtime', width: 80 },
            { label: '操作', name: 'sy', width: 70,align:'center',formatter: function(cellvalue, options, rowdata) {
                    return "<a style='cursor:pointer;' class='btn btn-primary' onclick='vm.update("+ rowdata.id +")'>" +
                        "详情</a>&nbsp;&nbsp;" +
                        "<a style='cursor:pointer;' class='btn btn-primary' onclick='vm.del("+ rowdata.id +")'>" +
                        "删除</a>";
                }}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q:{
            username:null,
            telphone:null
        },
		showList: true,
		title: null,
		feedback: {},
        imgurl: []
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.feedback = {};
		},
		update: function (id) {

			vm.showList = false;
            vm.title = "详情";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.feedback.id == null ? "sys/feedback/save" : "sys/feedback/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.feedback),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (id) {
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "sys/feedback/delete/"+id,
                        contentType: "application/json",
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(id){
            vm.imgurl = [];
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/feedback/info/"+id,
                success: function(r){
                    vm.feedback = r.feedback;
                    if(vm.feedback.imgurl!=null&&vm.feedback.imgurl!=''){
                        var array = vm.feedback.imgurl.split(",");
                        for(var i=0;i<array.length;i++){
                            vm.imgurl.push({name: array[i],url: array[i]});
                        }
                    }
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'username': vm.q.username,telphone:vm.q.telphone},
                page:page
            }).trigger("reloadGrid");
		}
	}
});